import {
    buildActiveTabs,
    buildDefaultValueListFn,
    buildFieldOptionsFn,
    buildRulesListFn, buildUploadDataFn
} from "@/utils/vue2js-generator";
import {traverseFieldWidgets} from "@/utils/util";

export const genVue3Ts = function (formConfig, widgetList) {
    let defaultValueList = []
    let rulesList = []
    let fieldOptions = []
    let uploadData = []
    traverseFieldWidgets(widgetList, (widget) => {
        buildDefaultValueListFn(formConfig, widgetList, defaultValueList)(widget)
        buildRulesListFn(formConfig, widgetList, rulesList)(widget)
        buildFieldOptionsFn(formConfig, widgetList, fieldOptions, true)(widget)
        buildUploadDataFn(formConfig, widgetList, uploadData, true)(widget)
    })

    const activeTabs = buildActiveTabs(formConfig, widgetList,true)

    const v3JSTemplate = `
import {ref} from "vue";
import type {FormInstance} from "element-plus";
import {type FormInstance, useLocale} from "element-plus";

const locale = useLocale()
const vForm = ref<FormInstance>();
const ${formConfig.modelName} =  ref({
   ${defaultValueList.join('\n')}
})
const ${formConfig.rulesName} = {
  ${rulesList.join('\n')}
}
${activeTabs.join('\n')}

${fieldOptions.join('\n')}

${uploadData.join('\n')}
const resetForm = () => {
  vForm.value!.resetFields()
}

const submitForm = async () => {
    if (!vForm.value!.validate()) {
        return
    }
    
    //TODO: 提交表单
}
`
    return v3JSTemplate
}

